package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldContentVo;
import com.zb.entity.ZjNetworkAloneAuthenFieldContent;
import com.zb.entity.ZjNetworkTeamAuthenFieldContent;
import com.zb.mapper.ZjNetworkAloneAuthenFieldContentMapper;
import com.zb.service.ZjNetworkAloneAuthenFieldContentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点个人认证字段内容表，当zj_network_alone_authen_field表字段type=3/4时，单选/多选 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-31
 */
@Service
public class ZjNetworkAloneAuthenFieldContentServiceImpl extends ServiceImpl<ZjNetworkAloneAuthenFieldContentMapper, ZjNetworkAloneAuthenFieldContent> implements ZjNetworkAloneAuthenFieldContentService {

    // 保存或更新单选/多选内容
    @Override
    public void save(Long custAccId, Long networkAloneAuthenFieldId, List<FieldContentVo> contentVos) {
        if (CollectionUtil.isEmpty(contentVos)) {
            return;
        }
        List<ZjNetworkAloneAuthenFieldContent> adds = new ArrayList<>();
        List<ZjNetworkAloneAuthenFieldContent> updates = new ArrayList<>();
        ZjNetworkAloneAuthenFieldContent content = null;
        for (FieldContentVo vo : contentVos) {
            content = new ZjNetworkAloneAuthenFieldContent();
            BeanUtil.copyProperties(vo, content);
            if (null != content.getId()) {
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setNetworkAloneAuthenFieldId(networkAloneAuthenFieldId);
            adds.add(content);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 获取单选/多选内容
    @Override
    public List<ZjNetworkAloneAuthenFieldContent> getByNetworkAloneAuthenFieldIds(List<Long> networkAloneAuthenFieldIds) {
        if (CollectionUtil.isEmpty(networkAloneAuthenFieldIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkAloneAuthenFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_alone_authen_field_id", "val")
                .in("network_alone_authen_field_id", networkAloneAuthenFieldIds);
        return this.list(wrapper);
    }

    // 获取单选/多选内容
    @Override
    public List<ZjNetworkAloneAuthenFieldContent> getByNetworkAloneAuthenFieldId(Long networkAloneAuthenFieldId) {
        List<Long> networkAloneAuthenFieldIds = new ArrayList<>();
        networkAloneAuthenFieldIds.add(networkAloneAuthenFieldId);
        return getByNetworkAloneAuthenFieldIds(networkAloneAuthenFieldIds);
    }

    // 根据认证字段ID
    @Override
    public void deleteBatchByNetworkAloneAuthenFieldIds(List<Long> networkAloneAuthenFieldIds) {
        if (CollectionUtil.isEmpty(networkAloneAuthenFieldIds)) {
            return;
        }
        QueryWrapper<ZjNetworkAloneAuthenFieldContent> wrapper = new QueryWrapper<>();
        wrapper.in("network_alone_authen_field_id", networkAloneAuthenFieldIds);
        this.remove(wrapper);
    }
}
